package io.renren.modules.classinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RenException;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.classinfo.dao.ClassInfoDao;
import io.renren.modules.classinfo.vo.ClassInfoVO;
import io.renren.modules.classinfo.dto.ClassInfoDTO;
import io.renren.modules.classinfo.entity.ClassInfoEntity;
import io.renren.modules.classinfo.service.ClassInfoService;
import io.renren.modules.grant.vo.NoticeTypeVO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 班级信息管理
 *
 * @author test test
 * @since 1.0.0 2024-09-13
 */
@Service
public class ClassInfoServiceImpl extends CrudServiceImpl<ClassInfoDao, ClassInfoEntity, ClassInfoVO> implements ClassInfoService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<ClassInfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String className = (String)params.get("className");
        String teacherId = (String)params.get("teacherId");

        QueryWrapper<ClassInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(className), "class_name", className);
        wrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        return wrapper;
    }

    @Override
    public void save(ClassInfoDTO classInfoDTO) {
        ClassInfoEntity classInfoEntity = ConvertUtils.sourceToTarget(classInfoDTO, ClassInfoEntity.class);
        this.insert(classInfoEntity);
    }

    @Override
    public void update(ClassInfoDTO classInfoDTO) {
        ClassInfoEntity classInfoEntity = ConvertUtils.sourceToTarget(classInfoDTO, ClassInfoEntity.class);
        this.updateById(classInfoEntity);
    }

    @Override
    public ClassInfoVO getByTeacherId(Long teacherId) {
        QueryWrapper<ClassInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        ClassInfoEntity classInfoEntity = this.baseDao.selectOne(wrapper);
        return ConvertUtils.sourceToTarget(classInfoEntity, ClassInfoVO.class);
    }

    @Override
    public PageData<ClassInfoVO> classInfoPage(Map<String, Object> params) {
        List<SysUserDTO> userDtoList = sysUserService.getAll();
        PageData<ClassInfoVO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creator = userDtoList.stream().filter(j -> j.getId().compareTo(e.getTeacherId()) == 0).findAny().orElse(null);
            e.setTeacherName(creator == null ? "-" : creator.getRealName());
        });
        return page;
    }

    @Override
    public void saveClassinfo(ClassInfoDTO dto) {
        List<ClassInfoEntity> entityList = this.baseDao.selectList(null);
        ClassInfoEntity classNameEntity = entityList.stream().filter(e -> e.getClassName().equals(dto.getClassName())).findFirst().orElse(null);
        if (classNameEntity != null) {
            throw new RenException("班级名称重复!");
        }
        ClassInfoEntity teacherEntity = entityList.stream().filter(e -> e.getTeacherId().compareTo(dto.getTeacherId()) == 0).findFirst().orElse(null);
        if (teacherEntity != null) {
            throw new RenException("改教师已匹配班级!");
        }
        this.save(dto);
    }

    @Override
    public void updateClassinfo(ClassInfoDTO dto) {
        QueryWrapper<ClassInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.ne("id", dto.getId());
        List<ClassInfoEntity> entityList = this.baseDao.selectList(wrapper);
        ClassInfoEntity classNameEntity = entityList.stream().filter(e -> e.getClassName().equals(dto.getClassName())).findFirst().orElse(null);
        if (classNameEntity != null) {
            throw new RenException("班级名称重复!");
        }
        ClassInfoEntity teacherEntity = entityList.stream().filter(e -> e.getTeacherId().compareTo(dto.getTeacherId()) == 0).findFirst().orElse(null);
        if (teacherEntity != null) {
            throw new RenException("改教师已匹配班级!");
        }
        this.update(dto);
    }

}